﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dcms.Common;

public partial class UserControl_SysControl_MynetPageList : System.Web.UI.UserControl
{
    private string FileName; //获得当的文件名
    private int _totalRows;//总记录数
    private int _totalPages;//总页数
    private int _pageSize;//每页大小
    private int _currentPageNO;//当前页数
    private int _firstPageNO = 1;//第1页数
    private int _lastPageNO;//最后页数
    private int _showPageMinNumber;//展示的最少页码
    private int _showPageMaxNumber;//展示的最大页码
    private int _pageRange = 6;//页长度
    protected string JsCurrentPageNo = "1";
    protected string JsTotalPage = "1";
    public int CurrentPageNO
    {
        get { return Convert.ToInt16(literCurrPage.Text); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            PageLoadData();
            JsCurrentPageNo= this._currentPageNO.ToString();
        }
    }
    protected void PageLoadData()
    {
        int TempTotalRos = 0, TempCurrPageNo = 0, TempPageSize = 0, TempPageRange = 0;
        if (Utils.GetCookie("TotalRecord") == "")
        {
            TempTotalRos = 0;
        }
        else
        {
            try
            {
                TempTotalRos = Convert.ToInt32(Utils.GetCookie("TotalRecord"));
            }
            catch
            {
                TempTotalRos = 0;
            }
        }
        if (Utils.GetCookie("CurrPageIndex") == "")
        {
            TempCurrPageNo = 0;
        }
        else
        {
            try
            {
                TempCurrPageNo = Convert.ToInt32(Utils.GetCookie("CurrPageIndex"));
            }
            catch
            {
                TempCurrPageNo = 0;
            }
        }
        if (Utils.GetCookie("PageSize") == "")
        {
            TempPageSize = 0;
        }
        else
        {
            try
            {
                TempPageSize = Convert.ToInt32(Utils.GetCookie("PageSize"));
            }
            catch
            {
                TempPageSize= 0;
            }
        }
        if (Utils.GetCookie("RadPage") == "")
        {
            TempPageRange = 0;
        }
        else
        {
            try
            {
                TempPageRange = Convert.ToInt32(Utils.GetCookie("RadPage"));
            }
            catch
            {
                TempPageRange = 0;
            }
        }
        InitData(TempTotalRos, TempCurrPageNo, TempPageSize, TempPageRange);
    }
    public void InitData(int Totalrows, int Pageno, int Pagesize, int PageRange)
    {
        this._totalRows = Totalrows;
        this._pageSize = Pagesize;
        this._totalPages = ValidTotalPage(Totalrows, Pagesize); //取得最大页数
        this._firstPageNO = 1;
        this._lastPageNO = this._totalPages;
        this._currentPageNO = ValidPageNO(Pageno);
        this._pageRange = PageRange;
        JsTotalPage= this._totalPages.ToString();
        CalculateShowPageNumber();
        BindPageInfo();
        RenderPc();
    }
    /// <summary>
    /// 检查总页数
    /// </summary>
    /// <param name="TotalRows">总行数</param>
    /// <param name="Pagesize">页大小</param>
    /// <returns>总页数</returns>
    public int ValidTotalPage(int TotalRows, int Pagesize)
    {
        if (TotalRows <= Pagesize)
        {
            return 1;
        }
        else
        {
            return ((TotalRows - 1) / Pagesize + 1);
        }
    }
    /// <summary>
    /// 检查当前页
    /// </summary>
    /// <param name="PageNo">当前页</param>
    /// <returns>当前页</returns>
    private int ValidPageNO(int PageNo)
    {
        if (PageNo <= this._firstPageNO)
        {
            return 1;
        }
        else
        {
            if (PageNo > this._totalPages)
            {
                return this._totalPages;
            }
            else
            {
                return PageNo;
            }
        }
    }
    protected void RenderPc()
    {
        lnkFirst.NavigateUrl = FileName + "Page=1";
        lnkPrev.NavigateUrl = FileName + "Page=" + Convert.ToString(this._currentPageNO - 1);
        lnkNext.NavigateUrl = FileName + "Page=" + Convert.ToString(this._currentPageNO + 1);
        lnkEnd.NavigateUrl = FileName + "Page=" + this._totalPages.ToString();//最后一页
        this.LiterPageAmount.Text = this._totalPages.ToString();//总页数
        this.LiterCount.Text = this._totalRows.ToString();//总记录数
        this.literCurrPage.Text = this._currentPageNO.ToString();
    }
    /// <summary>
    /// 新版产生分页,修改时间2008-03-22
    /// </summary>
    private void CalculateShowPageNumber()
    {
        if (this._totalPages <= this._pageRange)
        {
            this._showPageMinNumber = 1;
            this._showPageMaxNumber = this._totalPages;
        }
        else
        {
            if (this._currentPageNO - this._pageRange < 1)
            {
                this._showPageMinNumber = 1;
            }
            else
            {
                this._showPageMinNumber = this._currentPageNO - this._pageRange;
            }

            if (this._currentPageNO + this._pageRange >= this._totalPages)
            {
                this._showPageMaxNumber = this._totalPages;
            }
            else
            {
                this._showPageMaxNumber = this._currentPageNO + this._pageRange;
            }
        }

    }
    /// <summary>
    /// 产品数字分页
    /// </summary>
    private void BindPageInfo()
    {
        FileName = Request.Url.ToString();//CurrentExecutionFilePath是获取文件名
        FileName = FileName.Substring(FileName.LastIndexOf("/") + 1);
        if (FileName.IndexOf("Page=") > 0)//FileName.Substring(0, FileName.IndexOf("page="));
        {
            FileName = FileName.Substring(0, FileName.IndexOf("Page=") - 1);
        }
        FileName += (FileName.IndexOf("?") > 0) ? "&" : "?";
        LiterLinkList.Text = "";
        for (int i = _showPageMinNumber; i <= _showPageMaxNumber; i++)
        {
            if (i == this._currentPageNO)
            {
                LiterLinkList.Text += "<a class=\"leftCurrent\" href=" + FileName + "Page=" + i.ToString() + ">" + i.ToString() + "</a>";
            }
            else
            {
                LiterLinkList.Text += "<a href=" + FileName + "Page=" + i.ToString() + ">" + i.ToString() + "</a>";
            }
        }
    }
}
